//package com.itheima.grow.util;
//
//import com.alibaba.fastjson.JSONObject;
//import com.aliyuncs.CommonRequest;
//import com.aliyuncs.CommonResponse;
//import com.aliyuncs.IAcsClient;
//import com.aliyuncs.exceptions.ClientException;
//import com.aliyuncs.http.MethodType;
//import com.cdc.employee.workorder.model.entity.SmsSend;
//import com.cdc.employee.workorder.model.entity.SmsTemplate;
//import com.cdc.employee.workorder.model.enums.SendMethod;
//import com.cdc.employee.workorder.model.enums.SendStatus;
//import com.cdc.employee.workorder.service.ISmsSendService;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.collections4.MapUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Propagation;
//import org.springframework.transaction.annotation.Transactional;
//import org.springframework.util.ObjectUtils;
//
//import java.util.Date;
//import java.util.Map;
//阿里云短信4.0工具类
//@Component
//@Slf4j
//public class SmsSendUtil {
//
//    @Autowired
//    private IAcsClient iAcsClient;
//
//    @Autowired
//    private ISmsSendService smsSendService;
//
//    @Value("${aliyun.sms.enable:false}")
//    private boolean enable;
//
//    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
//    public void send(Map<String,Object> params, SmsTemplate template, SendMethod sendMethod) {
//        if (!enable || ObjectUtils.isEmpty(template) || MapUtils.isEmpty(params)) {
//            return;
//        }
//        //构建请求
//        CommonRequest request = new CommonRequest();
//        request.setMethod(MethodType.POST);
//        request.setDomain("dysmsapi.aliyuncs.com");//不要修改
//        request.setVersion("2017-05-25");//不要修改
//        request.setAction("SendSms");
//        request.putQueryParameter("PhoneNumbers", params.get("receipt").toString());//替要发送到的手机号码
//        request.putQueryParameter("SignName", "上汽大众");//短信签名
//        request.putQueryParameter("TemplateCode", template.getTemplateNo());//短信模板号
//        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(params));
//        CommonResponse response = null;
//        SmsSend send = new SmsSend();
//        send.setSendStatus(SendStatus.SUCCESS);
//        try {
//            log.info("发送短信参数：{}",JSONObject.toJSONString(request));
//            response = iAcsClient.getCommonResponse(request);
//            log.info("发送短信响应：{}",JSONObject.toJSONString(response));
//            JSONObject jsonObject = JSONObject.parseObject(response.getData());
//            String message = jsonObject.getString("Message");
//            String requestId = jsonObject.getString("RequestId");
//            String bizId = jsonObject.getString("BizId");
//            String code = jsonObject.getString("Code");
//            send.setRequestId(requestId);
//            send.setBizId(bizId);
//            if (!"OK".equals(message)) {
//                throw new ClientException(code,message,requestId);
//            }
//        } catch (ClientException e) {
//            send.setFailReason(e.getErrMsg());
//            send.setRequestId(e.getRequestId());
//            send.setBizId(e.getErrCode());
//            send.setSendStatus(SendStatus.FAIL);
//        } finally {
//            send.setTemplateNo(template.getTemplateNo());
//            send.setTemplateName(template.getTemplateName());
//            send.setRecipient(params.get("user_name").toString());
//            send.setRecipientPhone(params.get("receipt").toString());
//            send.setSendTime(new Date());
//            send.setSender(params.get("sender").toString());
//            send.setSendMethod(sendMethod);
//            smsSendService.save(send);
//        }
//    }
//
//}
